package com.wk.paradise.redis.utils;

import com.wk.paradise.redis.config.redisConfig.RedisPool;
import java.util.Collections;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Pool;

/**
 * @Classname RedisUtil
 * @Description 工具
 * @Date 2019/12/18 16:34
 * @Author by weikai
 */
public final class RedisUtil {

  private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class);

  private RedisUtil() {
  }

  public static String ping(RedisPool pool) {
    Jedis jedis = pool.getResource();
    try{
      return jedis.ping("jedis可以连接");
    }finally {
      jedis.close();
    }

  }

  public static Set<String> keys(Pool<Jedis> pool, final String pattern) {
    if (isEmpty(pattern)) {
      return Collections.emptySet();
    }
    Jedis jedis = pool.getResource();
    try {
      debugClientSetname(jedis, pattern);
      return jedis.keys(pattern);
    }finally {
      jedis.close();
    }

  }

  private static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  private static void debugClientSetname(Jedis jedis, String key) {
    if (LOG.isDebugEnabled()) {
      if (!isEmpty(key)) {
        jedis.clientSetname(key);
      }
    }
  }
}
